<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
if(!class_exists('CI_Model')) { class CI_Model extends Model {} }

class Faq_model extends CI_Model {
	var $perpage = 10;
	var $url;
	public function __construct() {
	   parent::__construct();
	   $this->url = $this->uri->segment(1);
	   	$this->input->enable_hspc();
	}
	public function index() {
		$data['from'] = $from = $this->input->get('from', true);
		if ($from == "") $from = 0;
		$data['pages'] = $this->db->select()->from('faq')->where('faq_status', '1')->order_by('faq_date', 'desc')->limit($this->perpage, $from)->get()->result_array();

		/* pagination */
		$count = $this->db->select()->from('faq')->where('faq_status', 1)->count_all_results();
		if ($from != 0 && sizeof($pages) <= 0 && $count > 0) {
			$pgng = $from - $this->perpage;
			redirect($this->url.'/?from='.$pgng);
		}
		$this->load->library('pagination');
		$config['base_url'] = '/'.$this->url.'/';
		$config['total_rows'] = $count;
		$config['page_query_string'] = TRUE;
		$config['per_page'] = $this->perpage;
		$this->pagination->initialize($config);
		$data['paging'] = $this->pagination->create_links();
		/* pagination */

		$form = new jQuickForm('pg_simple');

		$form->setLabel('Задать свой вопрос или оставить мнение');

		$gr = $form->insertGroup()->addClass('col col3');
		$gr->insertInputText('faq_name')->setLabel('Ваше имя')->addRuleRequired();

		$phone = $gr->insertInputText('faq_phone')->setLabel('Телефон');
		$phone->addRule('callback', 'Не верный формат поля', array('callback' => 'validate_phone'), HTML_QuickForm2_Rule::CLIENT);
		$phone->addRule('callback', 'Не верный формат поля', array('callback' => 'validate_phone'), HTML_QuickForm2_Rule::SERVER);
		$phone->addRuleRequired();

		$email = $gr->insertInputText('faq_email')->setLabel('Электронная почта');
		$email->addRule('callback', 'Не верный формат поля', array('callback' => 'validate_email'), HTML_QuickForm2_Rule::CLIENT);
		$email->addRule('callback', 'Не верный формат поля', array('callback' => 'validate_email'), HTML_QuickForm2_Rule::SERVER);
		$email->addRuleRequired();

		$form->insertInputText('faq_title')->setLabel('Тема обращения')->addRuleRequired();
		$form->insertTextArea('faq_text')->setLabel('Текст обращения')->addRuleRequired();

		$this->tools_model->captcha($form);

		if ($_SERVER['REQUEST_METHOD'] == "POST" && $form->validate()) {
			$mail_data = $p_data = $this->input->post();
			$hidden = sha1("soltbyme".$this->session->userdata('session_id')."mybigsolthash");
			$hidden2 = sha1("soltbyme".$this->session->userdata('session_id')."myhashsolt");
			unset($mail_data[$hidden]);
			unset($mail_data[$hidden2]);
			$mail_data['faq_id'] = 0;
			$mail_data['faq_date'] = time();
			$mail_data['faq_ip'] = $this->input->ip_address();
			$this->db->insert('faq', $mail_data);
			$mail_data['o'] = $this->db->insert_id();
			$this->load->library('email');
			$msg = $this->load->view('modules/faq/email', $mail_data, true);
			$this->email->from($this->config->item('from_email'), $this->config->item('from_name'));
			$this->email->to($this->config->item('admin_email'));
			$this->email->subject('Сообщение в модуле вопрос-ответ с сайта '.$this->config->item('company_name'));
			$this->email->message($msg);

			if ($this->email->send()) {
				$this->session->set_flashdata('msg', 'Спасибо, Ваше сообщение отправлено. Ответ на него появится на этой странице.');
			} else {
				$this->session->set_flashdata('msg', 'Ошибка отправки сообщения.');
			}
			redirect($this->url.'/');
		}

		$form->insertInputSubmit('Отправить');

		if ($this->ion_auth->logged_in()) {
			$user = $this->ion_auth->get_user();
			$have_data['faq_name'] = $user->first_name." ".$user->last_name;
			$have_data['faq_phone'] = $user->phone;
			$have_data['faq_email'] = $user->email;
		}

		$form->addDataSource(new HTML_QuickForm2_DataSource_Array($have_data));
		$data['form'] = $form."";
		$data['js'] = jQuickForm::js()->getJsAll();
		$out['text'] = $this->load->view('modules/faq/index', $data, true);
		return $out;
	}

}